import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';
import 'package:chewie/chewie.dart';

const TestVideoUrl = "https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4";

class VideoPlyer extends StatefulWidget {
  @override
  _VideoAppState createState() => _VideoAppState();
}

class _VideoAppState extends State<VideoPlyer> {
  VideoPlayerController _controller;
  String errorReason;
  ChewieController _chewieController;

  @override
  void initState() {
    _controller = VideoPlayerController.network(TestVideoUrl)
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      }, onError: (e) {
        errorReason = "加载失败";
        setState(() {});
      });
    errorReason = "";
    _chewieController = ChewieController(
      videoPlayerController: _controller,
      autoPlay: true,
      looping: true,
    );

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('视频'),
      ),
      body: Center(
        child: Chewie(
          controller: _chewieController,),
      ),);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }
}
